import { Body, Controller, Get, Header, Injectable, Post } from '@nestjs/common';
import { Index } from '../emr_imp';
import { Tableobj } from '../entity/tableobj';

@Controller()
export class BaseController {
  constructor(
    private readonly baseindex:Index,
  ) {
  }

  //单SQL操作
  @Post('base/executeSql')
  async ExecuteSql(@Body() exesql:string):Promise<boolean>{
    return await this.baseindex.ExecuteSql(exesql);
  }

  //单SQL操作
  @Post('base/executebatch')
  async ExecuteBatch(@Body() exesqls:any):Promise<boolean>{
    let tables=new Array<string>();

    for(let i=0;i<exesqls.length;i++)
    {
      let sql:string=exesqls[i].sql;
      tables.push(sql);
    }
    return this.baseindex.ExecuteBatch(tables);
  }

  //单数据集合查询
  @Post('base/getdataset')
  @Header("Access-Control-Allow-Origin", "*")
  @Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, HEAD")
  async GetDataSet(@Body() sqlobj:any):Promise<any>
  {
    return await this.baseindex.GetData(sqlobj.sql);
  }

  //多数据结合查询
  @Get('base/getdatasets')
  async GetDataSets(@Body() sqlobjs:any):Promise<any>
  {
    let tableobjs=new Array<Tableobj>();
    for(let i=0;i<sqlobjs.length;i++)
    {
      let tableobj=new Tableobj();
      tableobj.tablename=sqlobjs[i].tablename;
      tableobj.sql=sqlobjs[i].sql;
      tableobjs.push(tableobj);
    }
    return await this.baseindex.GetDatas(tableobjs);
  }

  @Post('base/testclob')
  async TestClob(@Body() Content:any):Promise<boolean>{
    let tid=Content.tid;
    let content=Content.content;
    return await this.baseindex.TestClob(tid,content);
  }
}